package com.amap.flutter.tmap;

import android.content.Context;

import com.amap.flutter.tmap.LifecycleProvider;
import com.tencent.tencentmap.mapsdk.maps.TencentMapOptions;
import com.amap.flutter.tmap.utils.ConvertUtil;
import com.amap.flutter.map.utils.LogUtil;
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition;
import com.tencent.tencentmap.mapsdk.maps.TencentMapInitializer;
import com.tencent.map.geolocation.TencentLocationManager;

import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class TencentMapPlatformViewFactory extends PlatformViewFactory {
    private static final String CLASS_NAME = "TencentMapPlatformViewFactory";
    private final BinaryMessenger binaryMessenger;
    private final LifecycleProvider lifecycleProvider;

    TencentMapPlatformViewFactory(BinaryMessenger binaryMessenger, LifecycleProvider lifecycleProvider) {
        super(StandardMessageCodec.INSTANCE);
        this.binaryMessenger = binaryMessenger;
        this.lifecycleProvider = lifecycleProvider;
    }

    @Override
    public PlatformView create(Context context, int viewId, Object args) {
        final TencentMapOptionsBuilder builder = new TencentMapOptionsBuilder();

        if(!TencentMapInitializer.getAgreePrivacy()){
            TencentMapInitializer.setAgreePrivacy(true);
            TencentLocationManager.setUserAgreePrivacy(true);
        }

        Map<String, Object> params = null;
        try {
            ConvertUtil.density = context.getResources().getDisplayMetrics().density;
            params = (Map<String, Object>) args;
            LogUtil.i(CLASS_NAME, "create params==>" + params);
            Object options = ((Map<String, Object>) args).get("options");
            if (null != options) {
                ConvertUtil.interpretTencentMapOptions(options, builder);
            }

            if (params.containsKey("initialCameraPosition")) {
                CameraPosition cameraPosition = ConvertUtil.toCameraPosition(params.get("initialCameraPosition"));
                builder.setCamera(cameraPosition);
            }

            if (params.containsKey("markersToAdd")) {
                builder.setInitialMarkers(params.get("markersToAdd"));
            }
            if (params.containsKey("polylinesToAdd")) {
                builder.setInitialPolylines(params.get("polylinesToAdd"));
            }

            if (params.containsKey("polygonsToAdd")) {
                builder.setInitialPolygons(params.get("polygonsToAdd"));
            }

            if (params.containsKey("clustersToAdd")) {
//                builder.setInitialClusters(params.get("clustersToAdd"));
            }

            if (params.containsKey("apiKey")) {
                if(!TencentMapInitializer.getAgreePrivacy()){
                    TencentMapInitializer.setAgreePrivacy(true);
                    TencentLocationManager.setUserAgreePrivacy(true);
                }
                ConvertUtil.checkApiKey(params.get("apiKey"));
            }
        } catch (Throwable e) {
            LogUtil.e(CLASS_NAME, "create", e);
        }
        return builder.build(viewId, context, binaryMessenger, lifecycleProvider);
    }
}